package com.wenlin.myproduction.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.wenlin.myproduction.R;

/**
 * @author cwl
 * @desc PK进度条布局，带翻动的文字
 * @Email 377979485@qq.com
 * @time 2023/7/10 18:11
 */
public class PKProgressLayout extends ConstraintLayout {
    private PKProgressView pv;
    private GoldView gvLeft, gvRight;
    private long totalLeft, totalRight;

    public PKProgressLayout(Context context) {
        this(context, null);
    }

    public PKProgressLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        View view = View.inflate(getContext(), R.layout.view_pk_progress, null);
        addView(view);
        pv = findViewById(R.id.pv);
        gvLeft = findViewById(R.id.gv_left);
        gvRight = findViewById(R.id.gv_right);
    }

    /**
     * 设置初始化数据
     * @param totalLeft 左侧总额
     * @param totalRight 右侧总额
     */
    public void setInitData(long totalLeft, long totalRight) {
        this.totalLeft = totalLeft;
        this.totalRight = totalRight;
        pv.setInitData(totalLeft, totalRight);
        gvLeft.jumpTo(totalLeft);
        gvRight.jumpTo(totalRight);
    }

    /**
     * 设置数据
     * @param totalLeft 左侧总额
     * @param totalRight 右侧总额
     */
    public void updateData(long totalLeft, long totalRight) {
//        LogU.v("PK进度条  left = " + totalLeft + " right = " + totalRight);
        long dLeft = totalLeft - this.totalLeft;
        long dRight = totalRight - this.totalRight;
        this.totalLeft = totalLeft;
        this.totalRight = totalRight;
        if (dLeft != 0) {
            addLeft(dLeft);
        }
        if (dRight != 0) {
            addRight(dRight);
        }
    }

    /**
     * 增加左侧总额
     * @param left 左侧值
     */
    public void addLeft(long left) {
        pv.addLeft(left);
        gvLeft.runTo(pv.getTotalLeft());
    }

    /**
     * 增加右侧总额
     * @param right 右侧值
     */
    public void addRight(long right) {
        pv.addRight(right);
        gvRight.runTo(pv.getTotalRight());
    }
}
